<?php
/**
 * DouPHP小程序模块
 * Author: cuihaifeng  qq 366131726 微信同号
 * 网址：www.zbxingke.com www.0533hf.com
 */
if (!defined('IN_DOUCO_API')) {
    die('Access Denied');
}


if (strexists($_SERVER['HTTP_REFERER'], 'https://servicewechat.com/')) {
    $referer_url = parse_url($_SERVER['HTTP_REFERER']);
    list($appid, $version) = explode('/', ltrim($referer_url['path'], '/'));
}



function wxapp_result($errno, $message, $data = '') {
    exit(json_encode(array(
        'errno' => $errno,
        'message' => $message,
        'data' => $data,
    )));
}

function wxapp_checkSign() {
    global $_GPC;

    $token_array = pdo_get('config',array('name'=>'hash_code'),'value');
    $token = $token_array['value'];
    unset($token_array);



    if (!empty($_GET) && !empty($_GPC['sign'])) {
        foreach ($_GET as $key => $get_value) {
            if (!empty($get_value) && $key != 'sign' && $get_value!='undefined') {
                $sign_list[$key] = $get_value;
            }
        }

        foreach ($_POST as $key => $get_value) {
            if (!empty($get_value) && $key != 'sign' && $get_value!='undefined') {
                $sign_list[$key] = $get_value;
            }
        }

        ksort($sign_list);
        $sign = http_build_query($sign_list, '', '&') . $token;

        if(md5(urldecode($sign)) == $_GPC['sign']){
            return true;
        }else{
            logging_run($sign);
            return false;
        }

    } else {
        return false;
    }
}

/**
 * @todo 支付待添加
 */
function wxapp_pay(){
    global $_W, $_GPC;
}

//if (!wxapp_checkSign() && $_GPC['from']!='callback') {
//
//    wxapp_result(-1,'签名错误');
//}

/**
 * 检查是否登录
 * @return bool
 */
function wxapp_check_login(){
    global $_GPC;
    return strexists($_GPC['state'], 'dousid-');
}

/**
 * 获取openid
 */
function wxapp_getopenid(){
    global $_GPC;
    return substr($_GPC['state'],7);
}

/**
 * 根据openid获取user
 * @param $openid
 */
function get_user_by_openid($openid){
    $res = pdo_get('user_openid',array('openid'=>$openid),'user_id');
    $user_id = $res['user_id'];
    return get_user($user_id);
}








